package com.woniu.teachmall04.controller;

import com.woniu.teachmall04.dto.CarItem;
import com.woniu.teachmall04.dto.OrderGroup;
import com.woniu.teachmall04.dto.OrderItem;
import com.woniu.teachmall04.entity.Address;
import com.woniu.teachmall04.entity.Users;
import com.woniu.teachmall04.service.AddressService;
import com.woniu.teachmall04.service.OrderService;
import com.woniu.teachmall04.service.ShopcarService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShopcarService shopcarService;
    @Autowired
    private AddressService addressService;

    @GetMapping("/orders")
    public String orders(Model model,HttpSession session) {
        Users user = (Users) session.getAttribute("loginUser");
        if (user == null) {
            return "login";
        }
        Integer userId = user.getId();
        //拿到该用户的订单数据并整理成页面所需格式
        List<OrderItem> orderlist = orderService.getOrder(userId);
        //整理成订单数组的形式
        //将订单明细列表转换为订单组列表
        Map<String, List<OrderItem>> orderMap = orderlist.stream()
                .collect(Collectors.groupingBy(OrderItem::getNumber));
        //将订单组列表转换为订单组列表
        List<OrderGroup> orderGroups = orderMap.entrySet().stream()
                .map(entry -> new OrderGroup(entry.getKey(), entry.getValue()))
                .collect(Collectors.toList());

        model.addAttribute("orderGroups", orderGroups);
        return "orders";
    }

    @PostMapping("/order/submit")
    public String submit(Integer addressId,String payType,Model model,HttpSession session) {
        Users user = (Users)session.getAttribute("loginUser");
        if(user == null) {
            return "login";
        }
        Integer userId = user.getId();
        //拿到新订单编号
        String OrderNo = orderService.submitOrder(userId,addressId,user.getTelphone(),payType,session);
        //拿到总金额以及支付类型
        Double totalPrice = shopcarService.getTotalPrice(userId);
        model.addAttribute("OrderNo", OrderNo);
        model.addAttribute("totalPrice", totalPrice);
        model.addAttribute("payType",payType);
        return "order_submit";
    }
    //订单支付
    @PostMapping("/order/pay/now")
    public String payOrder(String orderId,HttpSession session) {
        Users user = (Users) session.getAttribute("loginUser");
        if (user == null) {
            return "login";
        }
        Integer userId = user.getId();
        String result = orderService.payOrder(userId, orderId);
        System.out.println("支付result： "+result);
        if ("success".equals(result)) return "redirect:/orders";
        else return "error";
    }

    @GetMapping("/order/add")
    public String addOrder(Model model, HttpSession session) {
        Users user = (Users)session.getAttribute("loginUser");
        if(user==null){
            return "login";
        }
        //获取地址列表、购物车数据、购物车总价格
        //1. 地址列表
        List<Address> addresses = addressService.getAllAddresses(user.getId());
        //2. 购物车数据
        List<CarItem> carItems = shopcarService.getCar(user.getId());
        //3. 获取总价格
        Double totalPrice = shopcarService.getTotalPrice(user.getId());
        model.addAttribute("totalPrice", totalPrice);
        model.addAttribute("carItems", carItems);
        model.addAttribute("addresses", addresses);


        return "order_add";
    }
}
